package com.github.thinwonton.springboot.showcase.mybatisplus.config;

import com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusProperties;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@EnableConfigurationProperties(MybatisPlusProperties.class)
@MapperScan("com.github.thinwonton.springboot.showcase.mybatisplus.account.dao")
public class DataSourceConfig {

    private static final String[] MAPPER_LOCATIONS = new String[]{
            "classpath:mapper/*.xml"
    };

    private final ResourceLoader resourceLoader;
    private final ApplicationContext applicationContext;
    private final Interceptor[] interceptors;
    private final DatabaseIdProvider databaseIdProvider;

    private MybatisPlusProperties mybatisPlusProperties;

    @Autowired
    public DataSourceConfig(MybatisPlusProperties mybatisPlusProperties,
                            ResourceLoader resourceLoader,
                            ApplicationContext applicationContext,
                            ObjectProvider<Interceptor[]> interceptorsProvider,
                            ObjectProvider<DatabaseIdProvider> databaseIdProvider) {
        this.mybatisPlusProperties = mybatisPlusProperties;
        this.interceptors = interceptorsProvider.getIfAvailable();
        this.resourceLoader = resourceLoader;
        this.applicationContext = applicationContext;
        this.databaseIdProvider = databaseIdProvider.getIfAvailable();

    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        //设置mapper路径
        this.mybatisPlusProperties.setMapperLocations(MAPPER_LOCATIONS);

        SqlSessionFactory sqlSessionFactory = MybatisPlusBuilder.createSqlSessionFactory(
                dataSource, this.mybatisPlusProperties, this.resourceLoader, this.applicationContext, this.interceptors, this.databaseIdProvider);
        return sqlSessionFactory;
    }

    //    MyBatis 动态扫描
//    @Bean
//    public MapperScannerConfigurer mapperScannerConfigurer() {
//        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
//        String basePackage = "com.ason.db.mapper";
//        mapperScannerConfigurer.setBasePackage(basePackage);
//        return mapperScannerConfigurer;
//    }

    //    配置事务管理
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}